<?php session_start(); ?>
<?php
define("EW_PAGE_ID", "list", TRUE); // Page ID
define("EW_TABLE_NAME", 'tb_dailyrx', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "tb_dailyrxinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php include "db.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	header("Location: login.php");
	exit();
}
?>

<?php
if (@phpversion() >= '5.0.0' && (!@ini_get('register_long_arrays') || @ini_get('register_long_arrays') == '0' || strtolower(@ini_get('register_long_arrays')) == 'off')) // PHP5 with register_long_arrays off
{
	$HTTP_POST_VARS = $_POST;
	$HTTP_GET_VARS = $_GET;
	$HTTP_SERVER_VARS = $_SERVER;
	$HTTP_COOKIE_VARS = $_COOKIE;
	$HTTP_ENV_VARS = $_ENV;
	$HTTP_POST_FILES = $_FILES;
	if (isset($_SESSION)) $HTTP_SESSION_VARS = $_SESSION;
}
?>
<?php
$ewCurSec = 0; // Initialise

// User levels
define("ewAllowadd", 1, true);
define("ewAllowdelete", 2, true);
define("ewAllowedit", 4, true);
define("ewAllowview", 8, true);
define("ewAllowlist", 8, true);
define("ewAllowreport", 8, true);
define("ewAllowsearch", 8, true);
define("ewAllowadmin", 16, true);
?>
<?php
/* if (@$HTTP_SESSION_VARS["jacob_status"] <> "login") {
	header("Location:  login.php");
	exit();
} */
?>
<?php

// Initialize common variables
$x_MonthYear = Null; 
$ox_MonthYear = Null;
$x_ID = Null; 
$ox_ID = Null;
$x_PreviousID = Null; 
$ox_PreviousID = Null;
$x_Link = Null; 
$ox_Link = Null;
$x_XReading = Null; 
$ox_XReading = Null;
$x_YesterdayXReading = Null; 
$ox_YesterdayXReading = Null;
$x_DiffXreading = Null; 
$ox_DiffXreading = Null;
$x_CheckAmount = Null; 
$ox_CheckAmount = Null;
$x_LunchAmount = Null; 
$ox_LunchAmount = Null;
$x_OverAmount = Null; 
$ox_OverAmount = Null;
$x_CreditAmount = Null; 
$ox_CreditAmount = Null;
$x_PayoutAmount = Null; 
$ox_PayoutAmount = Null;
$x_OtherAdj = Null; 
$ox_OtherAdj = Null;
$x_TotalDrawAmount = Null; 
$ox_TotalDrawAmount = Null;
$x_PreviousDrawAmount = Null; 
$ox_PreviousDrawAmount = Null;
$x_CashDiff = Null; 
$ox_CashDiff = Null;
$x_CashAdj = Null; 
$ox_CashAdj = Null;
$x_RegisterTotal = Null; 
$ox_RegisterTotal = Null;
$x_Short_Over = Null; 
$ox_Short_Over = Null;
?>
<?php
$sExport = @$HTTP_GET_VARS["export"]; // Load Export Request
if ($sExport == "excel") {
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename=ViewMasterRegister.xls');
}
if ($sExport == "word") {
	header('Content-Type: application/vnd.ms-word');
	header('Content-Disposition: attachment; filename=ViewMasterRegister.doc');
}
if ($sExport == "csv") {
	header('Content-Type: application/csv');
	header('Content-Disposition: attachment; filename=ViewMasterRegister.csv');
}
?>
<?php
$nStartRec = 0;
$nStopRec = 0;
$nTotalRecs = 0;
$nRecCount = 0;
$nRecActual = 0;
$sKeyMaster = "";
$sDbWhereMaster = "";
$sSrchAdvanced = "";
$sDbWhereDetail = "";
$sSrchBasic = "";
$sSrchWhere = "";
$sDbWhere = "";
$sDefaultOrderBy = "";
$sDefaultFilter = "";
$sWhere = "";
$sGroupBy = "";
$sHaving = "";
$sOrderBy = "";
$sSqlMasterBase = "";
$sSqlMaster = "";
$sListTrJs = "";
$bEditRow = "";
$nEditRowCnt = "";
$sDeleteConfirmMsg = "";
$nDisplayRecs = 30;
$nRecRange = 10;

// Open connection to the database
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);

// Handle Reset Command
ResetCmd();

// Get Search Criteria for Advanced Search
SetUpAdvancedSearch();

// Get Search Criteria for Basic Search
SetUpBasicSearch();

// Build Search Criteria
if ($sSrchAdvanced != "") {
	$sSrchWhere = $sSrchAdvanced; // Advanced Search
}
elseif ($sSrchBasic != "") {
	$sSrchWhere = $sSrchBasic; // Basic Search
}

// Save Search Criteria
if ($sSrchWhere != "") {
	$HTTP_SESSION_VARS["ViewMasterRegister_searchwhere"] = $sSrchWhere;

	// Reset start record counter (new search)
	$nStartRec = 1;
	$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
}
else
{
	$sSrchWhere = @$HTTP_SESSION_VARS["ViewMasterRegister_searchwhere"];
}

// Build SQL
$sSql = "SELECT
/*Month Year*/
UPPER(DATE_FORMAT(LogDate,'%M %Y')) as MonthYear,
t.LogDate,
@PrevLogDate:=(select max(i.LogDate) from tb_cashlog i where t.RegisterId = i.RegisterId and i.LogDate < t.LogDate) as PrevLogDate,
t.ID,
/*PREVIOUS ID*/
@PreviousID:=ifnull((select max(i.ID) from tb_cashlog i where i.RegisterID = t.RegisterID and logdate = @PrevLogDate),0) as PreviousID,
/*Link*/
CONCAT(t.ID,'::',Date(LogDate),'::',upper(u.UserName),'::', r.RegisterID) as Link,
@XReading:=t.XReading as XReading,
/*YesterdayXReading*/
@YesterdayXReading:=ifnull((select sum(Xreading) from tb_cashlog where RegisterID = t.RegisterID and logdate = @PrevLogDate),0) as YesterdayXReading,
/*DiffXreading*/
@DiffXreading:=(@XReading - @YesterdayXReading) DiffXreading,

@CheckAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='Check' AND ParentId=t.ID),0) as CheckAmount,                   
@LunchAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='Lunch' AND ParentId=t.ID),0) as LunchAmount,
@OverAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='Overring' AND ParentId=t.ID),0) as OverAmount,
@CreditAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='Credit' AND ParentId=t.ID),0) as CreditAmount,
@PayoutAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='PayOut' AND ParentId=t.ID),0) as PayoutAmount,
@TotalDrawAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='CashCoins' AND ParentId=t.ID),0) as TotalDrawAmount,
@PreviousDrawAmount:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='CashCoins' AND ParentId=@PreviousID),0) as PreviousDrawAmount,
@CashDiff:= (@TotalDrawAmount- @PreviousDrawAmount) as  CashDiff,
@CashAdj:=ifnull((select sum(a.Value*l.NameValue*l.Direction) as Amount from tb_activity a, tb_lookups l where a.LookupID=l.ID AND l.lookup='Deposit' AND ParentId=@PreviousID),0) as CashAdj,
/*OTHER ADJ*/
@OtherAdj:=(@CheckAmount+@LunchAmount+@OverAmount+@CreditAmount+@PayoutAmount) as OtherAdj,
/* REGISTER TOTALS */
@RegisterTotal:= @CashDiff+@CashAdj+@OtherAdj as RegisterTotal,
/* SHORT OVER */
(@RegisterTotal - @DiffXreading ) as Short_Over
FROM tb_cashlog t, tb_users u, tb_register r";
		
$sDefaultFilter = "t.UserID = u.ID AND t.RegisterID = r.ID AND t.logdate >= (CAse when (select Date(ControlDate) from tb_controlview where status ='Y') is null then 
			Date('2000-01-01 00:00:00')
		else
			(select Date(ControlDate) from tb_controlview where status ='Y')
		end)
";

$sGroupBy = "";
$sHaving = "";
$sDefaultOrderBy = " r.RegisterID, t.LogDate ";

// Build WHERE condition
$sDbWhere = "";
if ($sDbWhereDetail <> "") {
	$sDbWhere .= "(" . $sDbWhereDetail . ") AND ";
}
if ($sSrchWhere <> "") {
	$sDbWhere .= "(" . $sSrchWhere . ") AND ";
}
if (strlen($sDbWhere) > 5) {
	$sDbWhere = substr($sDbWhere, 0, strlen($sDbWhere)-5); // Trim rightmost AND
}
$sWhere = "";
if ($sDefaultFilter <> "") {
	$sWhere .= "(" . $sDefaultFilter . ") AND ";
}
if ($sDbWhere <> "") {
	$sWhere .= "(" . $sDbWhere . ") AND ";
}
if (substr($sWhere, -5) == " AND ") {
	$sWhere = substr($sWhere, 0, strlen($sWhere)-5);
}
if ($sWhere != "") {
	$sSql .= " WHERE " . $sWhere;
}
if ($sGroupBy != "") {
	$sSql .= " GROUP BY " . $sGroupBy;
}
if ($sHaving != "") {
	$sSql .= " HAVING " . $sHaving;
}

// Set Up Sorting Order
$sOrderBy = " ";
SetUpSortOrder();
if ($sOrderBy != "") {
	if (strpos($sOrderBy, "ASC")>0 || strpos($sOrderBy, "DESC")>0){
		$sSql .= " ORDER BY " . $sOrderBy;
	}else{
		$sSql .= " ORDER BY " . $sOrderBy." DESC";
	}
}

/*echo $sOrderBy;
echo $sSql;*/
// Uncomment to show SQL for debugging
// Export Data only

if (($sExport == "xml") || ($sExport == "csv")) {
	ExportData($sExport, $sSql);
	phpmkr_db_close($conn);
	exit;
}
?>
<?php include ("../header.php") ?>
<script type="text/javascript" src="../ew.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "/"; // set date separator	

//-->
</script>
<script type="text/javascript">
<!--
var firstrowoffset = 1; // first data row start at
var tablename = 'ewlistmain'; // table name
var lastrowoffset = 0; // footer row
var usecss = false; // use css
var rowclass = 'ewTableRow'; // row class
var rowaltclass = 'ewTableAltRow'; // row alternate class
var rowmoverclass = 'ewTableHighlightRow'; // row mouse over class
var rowselectedclass = 'ewTableSelectRow'; // row selected class
var roweditclass = 'ewTableEditRow'; // row edit class
var rowcolor = '#FFFFFF'; // row color
var rowaltcolor = '#F5F5F5'; // row alternate color
var rowmovercolor = '#FFCCFF'; // row mouse over color
var rowselectedcolor = '#CCFFFF'; // row selected color
var roweditcolor = '#FFFF99'; // row edit color

//-->
</script>

<script type="text/javascript">
<!--
var EW_HTMLArea;

//-->
</script>
<script type="text/javascript">
<!--
function EW_selectKey(elem) {
	var f = elem.form;	
	if (!f.elements["key_d[]"]) return;
	if (f.elements["key_d[]"][0]) {
		for (var i=0; i<f.elements["key_d[]"].length; i++)
			f.elements["key_d[]"][i].checked = elem.checked;	
	} else {
		f.elements["key_d[]"].checked = elem.checked;	
	}
	if (f.elements["checkall"])
	{
		if (f.elements["checkall"][0])
		{
			for (var i = 0; i<f.elements["checkall"].length; i++)
				f.elements["checkall"][i].checked = elem.checked;
		} else {
			f.elements["checkall"].checked = elem.checked;
		}
	}
	ew_clickall(elem);
}
function EW_selected(elem) {
	var f = elem.form;	
	if (!f.elements["key_d[]"]) return false;
	if (f.elements["key_d[]"][0]) {
		for (var i=0; i<f.elements["key_d[]"].length; i++)
			if (f.elements["key_d[]"][i].checked) return true;
	} else {
		return f.elements["key_d[]"].checked;
	}
	return false;
}

//-->
</script>
<?php

// Set up Record Set
$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query: " . phpmkr_error() . '<br>SQL: ' . $sSql);
$nTotalRecs = phpmkr_num_rows($rs);
if ($nDisplayRecs <= 0) { // Display All Records
	$nDisplayRecs = $nTotalRecs;
}
$nStartRec = 1;
SetUpStartRec(); // Set Up Start Record Position
?>
<p><span class="phpmaker">CUSTOM VIEW: View Master Register
<?php if ($sExport == "") { ?>
&nbsp;&nbsp;<a href="ViewMasterRegisterlistV2.php?export=excel">Export to Excel</a>
&nbsp;&nbsp;<a href="ViewMasterRegisterlistV2.php?export=word">Export to Word</a>
&nbsp;&nbsp;<a href="ViewMasterRegisterlistV2.php?export=csv">Export to CSV</a>
<?php } ?>
</span></p>
<?php if ($sExport == "") { ?>
<form action="ViewMasterRegisterlistV2.php">
<table border="0" cellspacing="0" cellpadding="0" >
	<tr>
		<td><span class="phpmaker">
			<input type="text" name="psearch" size="20">
			<input type="Submit" name="Submit" value="Search &nbsp;(*)">&nbsp;&nbsp;
			<a href="ViewMasterRegisterlistV2.php?cmd=reset">Show all</a>&nbsp;&nbsp;
<!--			<a href="ViewMasterRegistersrch.php">Advanced Search</a>-->
		</span></td>
	</tr>
	<tr><td><span class="phpmaker"><input type="radio" name="psearchtype" value="" checked>Exact phrase&nbsp;&nbsp;<input type="radio" name="psearchtype" value="AND">All words&nbsp;&nbsp;<input type="radio" name="psearchtype" value="OR">Any word</span></td></tr>
</table>
</form>
<?php } ?>
<?php
if (@$HTTP_SESSION_VARS["ewmsg"] <> "") {
?>
<p><span class="phpmaker" style="color: Red;"><?php echo $HTTP_SESSION_VARS["ewmsg"]; ?></span></p>
<?php
	$HTTP_SESSION_VARS["ewmsg"] = ""; // Clear message
}
?>
<?php if ($nTotalRecs > 0)  { ?>
<form method="post">
<table id="ewlistmain" border="0" cellspacing="1" cellpadding="4" bgcolor="#CCCCCC">
	<!-- Table header -->
	<tr bgcolor="#666666" height="23px" >
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Month Year
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("MonthYear"); ?>" class="phpmaker" style="color: #FFFFFF;">Month Year&nbsp;(*)<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Link
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("Link"); ?>" class="phpmaker" style="color: #FFFFFF;">Link&nbsp;(*)<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
XReading
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("XReading"); ?>" class="phpmaker" style="color: #FFFFFF;">XReading<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
YstrDay XReading
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("YesterdayXReading"); ?>" class="phpmaker" style="color: #FFFFFF;">YstrDay XReading<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Diff Xreading
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("DiffXreading"); ?>" class="phpmaker" style="color: #FFFFFF;">Diff Xreading<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Check Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("CheckAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Check Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Lunch Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("LunchAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Lunch Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Over Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("OverAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Over Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Credit Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("CreditAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Credit Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Payout Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("PayoutAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Payout Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Other Adj
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("OtherAdj"); ?>" class="phpmaker" style="color: #FFFFFF;">Other Adj<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Total Draw Amt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("TotalDrawAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Total Draw Amt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Previous DrawAmt
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("PreviousDrawAmount"); ?>" class="phpmaker" style="color: #FFFFFF;">Previous DrawAmt<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Cash Diff
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("CashDiff"); ?>" class="phpmaker" style="color: #FFFFFF;">Cash Diff<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Cash Adj
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("CashAdj"); ?>" class="phpmaker" style="color: #FFFFFF;">Cash Adj<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
RegTotal
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("RegisterTotal"); ?>" class="phpmaker" style="color: #FFFFFF;">RegTotal<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
		<td valign="top"><span class="phpmaker" style="color: #FFFFFF;">
<?php if ($sExport <> "") { ?>
Short Over
<?php }else{ ?>
	<a href="ViewMasterRegisterlistV2.php?order=<?php echo urlencode("Short_Over"); ?>" class="phpmaker" style="color: #FFFFFF;">Short Over<?php if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] == "ASC") { ?><img src="../images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] == "DESC") { ?><img src="../images/sortdown.gif" width="10" height="9" border="0"><?php } ?></a>
<?php } ?>
		</span></td>
<?php if ($sExport == "") { ?>
<?php } ?>
	</tr>
<?php

// Avoid starting record > total records
if ($nStartRec > $nTotalRecs) {
	$nStartRec = $nTotalRecs;
}

// Set the last record to display
$nStopRec = $nStartRec + $nDisplayRecs - 1;

// Move to first record directly for performance reason
$nRecCount = $nStartRec - 1;
if (phpmkr_num_rows($rs) > 0) {
	phpmkr_data_seek($rs, $nStartRec -1);
}
$nRecActual = 0;
while (($row = @phpmkr_fetch_array($rs)) && ($nRecCount < $nStopRec)) {
	$nRecCount = $nRecCount + 1;
	if ($nRecCount >= $nStartRec) {
		$nRecActual++;

		// Set row color
		$sItemRowClass = " bgcolor=\"#FFFFFF\"";
		$sListTrJs = " onmouseover='ew_mouseover(this);' onmouseout='ew_mouseout(this);' onclick='ew_click(this);'";

		// Display alternate color for rows
		if ($nRecCount % 2 <> 1) {
			$sItemRowClass = " bgcolor=\"#F5F5F5\"";
		}
		$x_MonthYear = $row["MonthYear"];
		$x_ID = $row["ID"];
		$x_PreviousID = $row["PreviousID"];
		$x_Link = $row["Link"];
		$x_XReading = $row["XReading"];
		$x_YesterdayXReading = $row["YesterdayXReading"];
		$x_DiffXreading = $row["DiffXreading"];
		$x_CheckAmount = $row["CheckAmount"];
		$x_LunchAmount = $row["LunchAmount"];
		$x_OverAmount = $row["OverAmount"];
		$x_CreditAmount = $row["CreditAmount"];
		$x_PayoutAmount = $row["PayoutAmount"];
		$x_OtherAdj = $row["OtherAdj"];
		$x_TotalDrawAmount = $row["TotalDrawAmount"];
		$x_PreviousDrawAmount = $row["PreviousDrawAmount"];
		$x_CashDiff = $row["CashDiff"];
		$x_CashAdj = $row["CashAdj"];
		$x_RegisterTotal = $row["RegisterTotal"];
		$x_Short_Over = $row["Short_Over"];
?>
	<!-- Table body -->
	<tr<?php echo $sItemRowClass; ?><?php echo $sListTrJs; ?>>
		<!-- MonthYear -->
		<td nowrap="nowrap" ><span class="phpmaker">
<?php echo $x_MonthYear; ?>
</span></td>
		<td nowrap="nowrap"><span class="phpmaker">
<span class="phpmaker4"><?php echo "<a href=".chr(34)."ActivityLog.php?ID=".$x_ID."".chr(34)." >".$x_Link."</a>"; ?></span>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker2">
<?php echo $x_XReading; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker2">
<?php echo $x_YesterdayXReading; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker2">
<?php echo number_format($x_DiffXreading, 2, '.', ''); ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_CheckAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_LunchAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_OverAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_CreditAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_PayoutAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker3">
<?php echo $x_OtherAdj; ?>
</span></td>
<td nowrap="nowrap" align="right"><span class="phpmaker5">
<?php echo $x_TotalDrawAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker5">
<?php echo $x_PreviousDrawAmount; ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker5">
<?php echo number_format($x_CashDiff, 2, '.', ''); ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker">
<?php echo number_format($x_CashAdj, 2, '.', ''); ?>
</span></td>
		<td nowrap="nowrap" align="right"><span class="phpmaker">
<?php echo number_format($x_RegisterTotal, 2, '.', ''); ?>
</span></td>
		<td nowrap="nowrap" align="right">
		<?php if ($x_Short_Over<0) { ?>
			<span class="phpmaker_red">
		<?php
		}
		else
		{?>
			<span class="phpmaker">
		<?php 
		}
		?>

<?php echo number_format($x_Short_Over, 2, '.', ''); ?>
</span></td>
<?php if ($sExport == "") { ?>
<?php } ?>
	</tr>
<?php
	}
}
?>
</table>
</form>
<?php } ?>
<?php

// Close recordset and connection
phpmkr_free_result($rs);
phpmkr_db_close($conn);
?>
<?php if ($sExport == "") { ?>
<form action="ViewMasterRegisterlistV2.php" name="ewpagerform" id="ewpagerform">
<table bgcolor="" border="0" cellspacing="1" cellpadding="4" bgcolor="#CCCCCC">
	<tr>
		<td nowrap>
<?php
if ($nTotalRecs > 0) {
	$rsEof = ($nTotalRecs < ($nStartRec + $nDisplayRecs));
	$PrevStart = $nStartRec - $nDisplayRecs;
	if ($PrevStart < 1) { $PrevStart = 1; }
	$NextStart = $nStartRec + $nDisplayRecs;
	if ($NextStart > $nTotalRecs) { $NextStart = $nStartRec ; }
	$LastStart = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
	?>
	<table border="0" cellspacing="0" cellpadding="0"><tr><td><span class="phpmaker">Page&nbsp;</span></td>
<!--first page button-->
	<?php if ($nStartRec == 1) { ?>
	<td><img src="../images/firstdisab.gif" alt="First" width="16" height="16" border="0"></td>
	<?php } else { ?>
	<td><a href="ViewMasterRegisterlistV2.php?start=1"><img src="../images/first.gif" alt="First" width="16" height="16" border="0"></a></td>
	<?php } ?>
<!--previous page button-->
	<?php if ($PrevStart == $nStartRec) { ?>
	<td><img src="../images/prevdisab.gif" alt="Previous" width="16" height="16" border="0"></td>
	<?php } else { ?>
	<td><a href="ViewMasterRegisterlistV2.php?start=<?php echo $PrevStart; ?>"><img src="../images/prev.gif" alt="Previous" width="16" height="16" border="0"></a></td>
	<?php } ?>
<!--current page number-->
	<td><input type="text" name="pageno" value="<?php echo intval(($nStartRec-1)/$nDisplayRecs+1); ?>" size="4"></td>
<!--next page button-->
	<?php if ($NextStart == $nStartRec) { ?>
	<td><img src="../images/nextdisab.gif" alt="Next" width="16" height="16" border="0"></td>
	<?php } else { ?>
	<td><a href="ViewMasterRegisterlistV2.php?start=<?php echo $NextStart; ?>"><img src="../images/next.gif" alt="Next" width="16" height="16" border="0"></a></td>
	<?php  } ?>
<!--last page button-->
	<?php if ($LastStart == $nStartRec) { ?>
	<td><img src="../images/lastdisab.gif" alt="Last" width="16" height="16" border="0"></td>
	<?php } else { ?>
	<td><a href="ViewMasterRegisterlistV2.php?start=<?php echo $LastStart; ?>"><img src="../images/last.gif" alt="Last" width="16" height="16" border="0"></a></td>
	<?php } ?>
	<td><span class="phpmaker">&nbsp;of <?php echo intval(($nTotalRecs-1)/$nDisplayRecs+1);?></span></td>
	</tr></table>
	<?php if ($nStartRec > $nTotalRecs) { $nStartRec = $nTotalRecs; }
	$nStopRec = $nStartRec + $nDisplayRecs - 1;
	$nRecCount = $nTotalRecs - 1;
	if ($rsEof) { $nRecCount = $nTotalRecs; }
	if ($nStopRec > $nRecCount) { $nStopRec = $nRecCount; } ?>
	<span class="phpmaker">Records <?php echo $nStartRec; ?> to <?php echo $nStopRec; ?> of <?php echo $nTotalRecs; ?></span>
<?php } else { ?>
	<span class="phpmaker">No records found</span>
<?php } ?>
		</td>
	</tr>
</table>
</form>
<?php } ?>
<?php if ($sExport <> "word" && $sExport <> "excel") { ?>
<?php include ("../footer.php") ?>
<?php } ?>
<?php

//-------------------------------------------------------------------------------
// Function SetUpAdvancedSearch
// - Set up Advanced Search parameter based on querystring parameters from Advanced Search Page
// - Variables setup: sSrchAdvanced

function SetUpAdvancedSearch()
{
	global $HTTP_GET_VARS;
	global $sSrchAdvanced;

	// Field MonthYear
	$x_MonthYear = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_MonthYear"]) : @$HTTP_GET_VARS["x_MonthYear"];
	$arrFldOpr = "";
	$z_MonthYear = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_MonthYear"]) : @$HTTP_GET_VARS["z_MonthYear"];
	$arrFldOpr = split(",",$z_MonthYear);
	if ($x_MonthYear <> "") {
		$sSrchAdvanced .= "UPPER(DATE_FORMAT(LogDate, '%M %Y')) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_MonthYear) : $x_MonthYear; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field ID
	$x_ID = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_ID"]) : @$HTTP_GET_VARS["x_ID"];
	$arrFldOpr = "";
	$z_ID = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_ID"]) : @$HTTP_GET_VARS["z_ID"];
	$arrFldOpr = split(",",$z_ID);
	if ($x_ID <> "") {
		$sSrchAdvanced .= "t.ID "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_ID) : $x_ID; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field PreviousID
	$x_PreviousID = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_PreviousID"]) : @$HTTP_GET_VARS["x_PreviousID"];
	$arrFldOpr = "";
	$z_PreviousID = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_PreviousID"]) : @$HTTP_GET_VARS["z_PreviousID"];
	$arrFldOpr = split(",",$z_PreviousID);
	if ($x_PreviousID <> "") {
		$sSrchAdvanced .= "`PreviousID` "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_PreviousID) : $x_PreviousID; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field Link
	$x_Link = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_Link"]) : @$HTTP_GET_VARS["x_Link"];
	$arrFldOpr = "";
	$z_Link = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_Link"]) : @$HTTP_GET_VARS["z_Link"];
	$arrFldOpr = split(",",$z_Link);
	if ($x_Link <> "") {
		$sSrchAdvanced .= "CONCAT(Date(LogDate), '::', upper(u.UserName), '::', r.RegisterID) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_Link) : $x_Link; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field XReading
	$x_XReading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_XReading"]) : @$HTTP_GET_VARS["x_XReading"];
	$arrFldOpr = "";
	$z_XReading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_XReading"]) : @$HTTP_GET_VARS["z_XReading"];
	$arrFldOpr = split(",",$z_XReading);
	if ($x_XReading <> "") {
		$sSrchAdvanced .= "t.XReading "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_XReading) : $x_XReading; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field YesterdayXReading
	$x_YesterdayXReading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_YesterdayXReading"]) : @$HTTP_GET_VARS["x_YesterdayXReading"];
	$arrFldOpr = "";
	$z_YesterdayXReading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_YesterdayXReading"]) : @$HTTP_GET_VARS["z_YesterdayXReading"];
	$arrFldOpr = split(",",$z_YesterdayXReading);
	if ($x_YesterdayXReading <> "") {
		$sSrchAdvanced .= "ifnull((select Xreading from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_YesterdayXReading) : $x_YesterdayXReading; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field DiffXreading
	$x_DiffXreading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_DiffXreading"]) : @$HTTP_GET_VARS["x_DiffXreading"];
	$arrFldOpr = "";
	$z_DiffXreading = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_DiffXreading"]) : @$HTTP_GET_VARS["z_DiffXreading"];
	$arrFldOpr = split(",",$z_DiffXreading);
	if ($x_DiffXreading <> "") {
		$sSrchAdvanced .= "(t.XReading - ifnull((select Xreading from tb_cashlog where   RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0)) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_DiffXreading) : $x_DiffXreading; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field CheckAmount
	$x_CheckAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_CheckAmount"]) : @$HTTP_GET_VARS["x_CheckAmount"];
	$arrFldOpr = "";
	$z_CheckAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_CheckAmount"]) : @$HTTP_GET_VARS["z_CheckAmount"];
	$arrFldOpr = split(",",$z_CheckAmount);
	if ($x_CheckAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_CheckAmount) : $x_CheckAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field LunchAmount
	$x_LunchAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_LunchAmount"]) : @$HTTP_GET_VARS["x_LunchAmount"];
	$arrFldOpr = "";
	$z_LunchAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_LunchAmount"]) : @$HTTP_GET_VARS["z_LunchAmount"];
	$arrFldOpr = split(",",$z_LunchAmount);
	if ($x_LunchAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_LunchAmount) : $x_LunchAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field OverAmount
	$x_OverAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_OverAmount"]) : @$HTTP_GET_VARS["x_OverAmount"];
	$arrFldOpr = "";
	$z_OverAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_OverAmount"]) : @$HTTP_GET_VARS["z_OverAmount"];
	$arrFldOpr = split(",",$z_OverAmount);
	if ($x_OverAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_OverAmount) : $x_OverAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field CreditAmount
	$x_CreditAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_CreditAmount"]) : @$HTTP_GET_VARS["x_CreditAmount"];
	$arrFldOpr = "";
	$z_CreditAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_CreditAmount"]) : @$HTTP_GET_VARS["z_CreditAmount"];
	$arrFldOpr = split(",",$z_CreditAmount);
	if ($x_CreditAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_CreditAmount) : $x_CreditAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field PayoutAmount
	$x_PayoutAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_PayoutAmount"]) : @$HTTP_GET_VARS["x_PayoutAmount"];
	$arrFldOpr = "";
	$z_PayoutAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_PayoutAmount"]) : @$HTTP_GET_VARS["z_PayoutAmount"];
	$arrFldOpr = split(",",$z_PayoutAmount);
	if ($x_PayoutAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_PayoutAmount) : $x_PayoutAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field OtherAdj
	$x_OtherAdj = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_OtherAdj"]) : @$HTTP_GET_VARS["x_OtherAdj"];
	$arrFldOpr = "";
	$z_OtherAdj = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_OtherAdj"]) : @$HTTP_GET_VARS["z_OtherAdj"];
	$arrFldOpr = split(",",$z_OtherAdj);
	if ($x_OtherAdj <> "") {
		$sSrchAdvanced .= "(ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0)) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_OtherAdj) : $x_OtherAdj; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field TotalDrawAmount
	$x_TotalDrawAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_TotalDrawAmount"]) : @$HTTP_GET_VARS["x_TotalDrawAmount"];
	$arrFldOpr = "";
	$z_TotalDrawAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_TotalDrawAmount"]) : @$HTTP_GET_VARS["z_TotalDrawAmount"];
	$arrFldOpr = split(",",$z_TotalDrawAmount);
	if ($x_TotalDrawAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_TotalDrawAmount) : $x_TotalDrawAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field PreviousDrawAmount
	$x_PreviousDrawAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_PreviousDrawAmount"]) : @$HTTP_GET_VARS["x_PreviousDrawAmount"];
	$arrFldOpr = "";
	$z_PreviousDrawAmount = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_PreviousDrawAmount"]) : @$HTTP_GET_VARS["z_PreviousDrawAmount"];
	$arrFldOpr = split(",",$z_PreviousDrawAmount);
	if ($x_PreviousDrawAmount <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_PreviousDrawAmount) : $x_PreviousDrawAmount; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field CashDiff
	$x_CashDiff = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_CashDiff"]) : @$HTTP_GET_VARS["x_CashDiff"];
	$arrFldOpr = "";
	$z_CashDiff = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_CashDiff"]) : @$HTTP_GET_VARS["z_CashDiff"];
	$arrFldOpr = split(",",$z_CashDiff);
	if ($x_CashDiff <> "") {
		$sSrchAdvanced .= "(ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_CashDiff) : $x_CashDiff; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field CashAdj
	$x_CashAdj = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_CashAdj"]) : @$HTTP_GET_VARS["x_CashAdj"];
	$arrFldOpr = "";
	$z_CashAdj = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_CashAdj"]) : @$HTTP_GET_VARS["z_CashAdj"];
	$arrFldOpr = split(",",$z_CashAdj);
	if ($x_CashAdj <> "") {
		$sSrchAdvanced .= "ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_CashAdj) : $x_CashAdj; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field RegisterTotal
	$x_RegisterTotal = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_RegisterTotal"]) : @$HTTP_GET_VARS["x_RegisterTotal"];
	$arrFldOpr = "";
	$z_RegisterTotal = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_RegisterTotal"]) : @$HTTP_GET_VARS["z_RegisterTotal"];
	$arrFldOpr = split(",",$z_RegisterTotal);
	if ($x_RegisterTotal <> "") {
		$sSrchAdvanced .= "((ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)) + ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) + (ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0))) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_RegisterTotal) : $x_RegisterTotal; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}

	// Field Short_Over
	$x_Short_Over = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["x_Short_Over"]) : @$HTTP_GET_VARS["x_Short_Over"];
	$arrFldOpr = "";
	$z_Short_Over = (get_magic_quotes_gpc()) ? stripslashes(@$HTTP_GET_VARS["z_Short_Over"]) : @$HTTP_GET_VARS["z_Short_Over"];
	$arrFldOpr = split(",",$z_Short_Over);
	if ($x_Short_Over <> "") {
		$sSrchAdvanced .= "((ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  and RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)) + ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) + (ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0))) - ((t.XReading - ifnull((select Xreading from tb_cashlog where   RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0))) "; // Add field
		$sSrchAdvanced .= @$arrFldOpr[0] . " "; // Add operator
		if (count($arrFldOpr) >= 1) {
			$sSrchAdvanced .= @$arrFldOpr[1]; // Add search prefix
		}
		$sSrchAdvanced .= (!get_magic_quotes_gpc()) ? addslashes($x_Short_Over) : $x_Short_Over; // Add input parameter
		if (count($arrFldOpr) >=2) {
			$sSrchAdvanced .= @$arrFldOpr[2]; // Add search suffix
		}
		$sSrchAdvanced .= " AND ";
	}
	if (strlen($sSrchAdvanced) > 4) {
		$sSrchAdvanced = substr($sSrchAdvanced, 0, strlen($sSrchAdvanced)-4);
	}
}

//-------------------------------------------------------------------------------
// Function BasicSearchSQL
// - Build WHERE clause for a keyword

function BasicSearchSQL($Keyword)
{
	$sKeyword = (!get_magic_quotes_gpc()) ? addslashes($Keyword) : $Keyword;
	$BasicSearchSQL = "";
	$BasicSearchSQL.= "UPPER(DATE_FORMAT(LogDate, '%M %Y')) LIKE '%" . $sKeyword . "%' OR ";
	$BasicSearchSQL.= "CONCAT(Date(LogDate), '::', upper(u.UserName), '::',  r.RegisterID) LIKE '%" . $sKeyword . "%' OR ";
	if (substr($BasicSearchSQL, -4) == " OR ") { $BasicSearchSQL = substr($BasicSearchSQL, 0, strlen($BasicSearchSQL)-4); }
	return $BasicSearchSQL;
}

//-------------------------------------------------------------------------------
// Function SetUpBasicSearch
// - Set up Basic Search parameter based on form elements pSearch & pSearchType
// - Variables setup: sSrchBasic

function SetUpBasicSearch()
{
	global $HTTP_GET_VARS;
	global $sSrchBasic;
	$sSearch = (!get_magic_quotes_gpc()) ? addslashes(@$HTTP_GET_VARS["psearch"]) : @$HTTP_GET_VARS["psearch"];
	$sSearchType = @$HTTP_GET_VARS["psearchtype"];
	if ($sSearch <> "") {
		if ($sSearchType <> "") {
			while (strpos($sSearch, "  ") != false) {
				$sSearch = str_replace("  ", " ",$sSearch);
			}
			$arKeyword = split(" ", trim($sSearch));
			foreach ($arKeyword as $sKeyword)
			{
				$sSrchBasic .= "(" . BasicSearchSQL($sKeyword) . ") " . $sSearchType . " ";
			}
		}
		else
		{
			$sSrchBasic = BasicSearchSQL($sSearch);
		}
	}
	if (substr($sSrchBasic, -4) == " OR ") { $sSrchBasic = substr($sSrchBasic, 0, strlen($sSrchBasic)-4); }
	if (substr($sSrchBasic, -5) == " AND ") { $sSrchBasic = substr($sSrchBasic, 0, strlen($sSrchBasic)-5); }
}

//-------------------------------------------------------------------------------
// Function SetUpSortOrder
// - Set up Sort parameters based on Sort Links clicked
// - Variables setup: sOrderBy, Session("Table_OrderBy"), Session("Table_Field_Sort")

function SetUpSortOrder()
{
	global $HTTP_SESSION_VARS;
	global $HTTP_GET_VARS;
	global $sOrderBy;
	global $sDefaultOrderBy;

	// Check for an Order parameter
	if (strlen(@$HTTP_GET_VARS["order"]) > 0) {
		$sOrder = @$HTTP_GET_VARS["order"];

		// Field MonthYear
		if ($sOrder == "MonthYear") {
			$sSortField = "UPPER(DATE_FORMAT(LogDate, '%M %Y'))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] = ""; }
		}

		// Field ID
		if ($sOrder == "ID") {
			$sSortField = "t.ID";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"] = ""; }
		}

		// Field PreviousID
		if ($sOrder == "PreviousID") {
			$sSortField = "`PreviousID`";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"] = ""; }
		}

		// Field Link
		if ($sOrder == "Link") {
			$sSortField = "CONCAT(Date(LogDate), '::', upper(u.UserName), '::',  r.RegisterID)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] = ""; }
		}

		// Field XReading
		if ($sOrder == "XReading") {
			$sSortField = "t.XReading";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] = ""; }
		}

		// Field YesterdayXReading
		if ($sOrder == "YesterdayXReading") {
			$sSortField = "ifnull((select Xreading from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] = ""; }
		}

		// Field DiffXreading
		if ($sOrder == "DiffXreading") {
			$sSortField = "(t.XReading - ifnull((select Xreading from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] = ""; }
		}

		// Field CheckAmount
		if ($sOrder == "CheckAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] = ""; }
		}

		// Field LunchAmount
		if ($sOrder == "LunchAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] = ""; }
		}

		// Field OverAmount
		if ($sOrder == "OverAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] = ""; }
		}

		// Field CreditAmount
		if ($sOrder == "CreditAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] = ""; }
		}

		// Field PayoutAmount
		if ($sOrder == "PayoutAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] = ""; }
		}

		// Field OtherAdj
		if ($sOrder == "OtherAdj") {
			$sSortField = "(ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] = ""; }
		}

		// Field TotalDrawAmount
		if ($sOrder == "TotalDrawAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] = ""; }
		}

		// Field PreviousDrawAmount
		if ($sOrder == "PreviousDrawAmount") {
			$sSortField = "ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] = ""; }
		}

		// Field CashDiff
		if ($sOrder == "CashDiff") {
			$sSortField = "(ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] = ""; }
		}

		// Field CashAdj
		if ($sOrder == "CashAdj") {
			$sSortField = "ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0)";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] = ""; }
		}

		// Field RegisterTotal
		if ($sOrder == "RegisterTotal") {
			$sSortField = "((ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)) + ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) + (ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0)))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] = ""; }
		}

		// Field Short_Over
		if ($sOrder == "Short_Over") {
			$sSortField = "((ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) - ifnull((select sum(Amount) from tb_master_register where ParentID = ((select ID from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)))), 0)) + ifnull((select sum(Amount) from tb_master_register where ParentID = t.ID), 0) + (ifnull((select sum(Amount) from tb_checks where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_lunchamt where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_overrings where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_creditcard where ParentID = t.ID), 0) + ifnull((select sum(Amount) from tb_payouts where ParentID = t.ID), 0))) - ((t.XReading - ifnull((select Xreading from tb_cashlog where  RegisterID = t.RegisterID and logdate = adddate(t.logdate, -1)), 0)))";
			$sLastSort = @$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"];
			if ($sLastSort == "ASC") { $sThisSort = "DESC"; } else{  $sThisSort = "ASC"; }
			$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] = $sThisSort;
		}
		else
		{
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] <> "") { @$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] = ""; }
		}
		$HTTP_SESSION_VARS["ViewMasterRegister_OrderBy"] = $sSortField . " " . $sThisSort;
		$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = 1;
	}
	$sOrderBy = @$HTTP_SESSION_VARS["ViewMasterRegister_OrderBy"];
	if ($sOrderBy == "") {
		$sOrderBy = $sDefaultOrderBy;
		$HTTP_SESSION_VARS["ViewMasterRegister_OrderBy"] = $sOrderBy;
	}
}

//-------------------------------------------------------------------------------
// Function SetUpStartRec
//- Set up Starting Record parameters based on Pager Navigation
// - Variables setup: nStartRec

function SetUpStartRec()
{

	// Check for a START parameter
	global $HTTP_SESSION_VARS;
	global $HTTP_GET_VARS;
	global $nStartRec;
	global $nDisplayRecs;
	global $nTotalRecs;
	if (strlen(@$HTTP_GET_VARS["start"]) > 0) {
		$nStartRec = @$HTTP_GET_VARS["start"];
		$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
	}elseif (strlen(@$HTTP_GET_VARS["pageno"]) > 0) {
		$nPageNo = @$HTTP_GET_VARS["pageno"];
		if (is_numeric($nPageNo)) {
			$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
			if ($nStartRec <= 0) {
				$nStartRec = 1;
			}elseif ($nStartRec >= (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1) {
				$nStartRec = (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
			}
			$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
		}else{
			$nStartRec = @$HTTP_SESSION_VARS["ViewMasterRegister_REC"];
			if  (!(is_numeric($nStartRec)) || ($nStartRec == "")) {
				$nStartRec = 1; // Reset start record counter
				$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
			}
		}
	}else{
		$nStartRec = @$HTTP_SESSION_VARS["ViewMasterRegister_REC"];
		if (!(is_numeric($nStartRec)) || ($nStartRec == "")) {
			$nStartRec = 1; //Reset start record counter
			$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
		}
	}
}

//-------------------------------------------------------------------------------
// Function ResetCmd
// - Clear list page parameters
// - RESET: reset search parameters
// - RESETALL: reset search & master/detail parameters
// - RESETSORT: reset sort parameters

function ResetCmd()
{
		global $HTTP_SESSION_VARS;
		global $HTTP_GET_VARS;

	// Get Reset Cmd
	if (strlen(@$HTTP_GET_VARS["cmd"]) > 0) {
		$sCmd = @$HTTP_GET_VARS["cmd"];

		// Reset Search Criteria
		if (strtoupper($sCmd) == "RESET") {
			$sSrchWhere = "";
			$HTTP_SESSION_VARS["ViewMasterRegister_searchwhere"] = $sSrchWhere;

		// Reset Search Criteria & Session Keys
		}elseif (strtoupper($sCmd) == "RESETALL") {
			$sSrchWhere = "";
			$HTTP_SESSION_VARS["ViewMasterRegister_searchwhere"] = $sSrchWhere;

		// Reset Sort Criteria
		}
		elseif (strtoupper($sCmd) == "RESETSORT") {
			$sOrderBy = "";
			$HTTP_SESSION_VARS["ViewMasterRegister_OrderBy"] = $sOrderBy;
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_MonthYear_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_ID_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousID_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_Link_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_XReading_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_YesterdayXReading_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_DiffXreading_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_CheckAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_LunchAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_OverAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_CreditAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_PayoutAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_OtherAdj_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_TotalDrawAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_PreviousDrawAmount_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_CashDiff_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_CashAdj_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_RegisterTotal_Sort"] = ""; }
			if (@$HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] <> "") { $HTTP_SESSION_VARS["ViewMasterRegister_x_Short_Over_Sort"] = ""; }
		}

		// Reset Start Position (Reset Command)
		$nStartRec = 1;
		$HTTP_SESSION_VARS["ViewMasterRegister_REC"] = $nStartRec;
	}
}

//-------------------------------------------------------------------------------
// Function ExportData
// - Export Data in Xml or Csv format

function ExportData($sExport, $sSql)
{
	global $conn;
	global $nDisplayRecs;
	global $nStartRec;
	global $nTotalRecs;
	$rs = phpmkr_query($sSql, $conn) or die("Failed to execute query: " . phpmkr_error() . '<br>SQL: ' . $sSql);
	$nTotalRecs = phpmkr_num_rows($rs);
	$nStartRec = 1;
	SetUpStartRec(); // Set Up Start Record Position
	$sCsvStr = NULL;
	$bExcelCSV = true; // if true, replace single double quote to 2 double quotes
	if ($sExport == "csv") {
		$sCsvStr .= "MonthYear" . ",";
		$sCsvStr .= "ID" . ",";
		$sCsvStr .= "PreviousID" . ",";
		$sCsvStr .= "Link" . ",";
		$sCsvStr .= "XReading" . ",";
		$sCsvStr .= "YesterdayXReading" . ",";
		$sCsvStr .= "DiffXreading" . ",";
		$sCsvStr .= "CheckAmount" . ",";
		$sCsvStr .= "LunchAmount" . ",";
		$sCsvStr .= "OverAmount" . ",";
		$sCsvStr .= "CreditAmount" . ",";
		$sCsvStr .= "PayoutAmount" . ",";
		$sCsvStr .= "OtherAdj" . ",";
		$sCsvStr .= "TotalDrawAmount" . ",";
		$sCsvStr .= "PreviousDrawAmount" . ",";
		$sCsvStr .= "CashDiff" . ",";
		$sCsvStr .= "CashAdj" . ",";
		$sCsvStr .= "RegisterTotal" . ",";
		$sCsvStr .= "Short_Over" . ",";
		$sCsvStr = substr($sCsvStr, 0, strlen($sCsvStr)-1); // Remove last comma
		$sCsvStr .= "\n";
	}

	// Avoid starting record > total records
	if ($nStartRec > $nTotalRecs) {
		$nStartRec = $nTotalRecs;
	}

	// Set the last record to display
	if ($nDisplayRecs <= 0) {
		$nStopRec = $nTotalRecs;
	} else {
		$nStopRec = $nStartRec + $nDisplayRecs - 1;
	}

	// Move to first record directly for performance reason
	$nRecCount = $nStartRec - 1;
	if (phpmkr_num_rows($rs) > 0) {
		phpmkr_data_seek($rs,$nStartRec -1);
	}
	$nRecActual = 0;
	while (($row = @phpmkr_fetch_array($rs)) && ($nRecCount < $nStopRec)) {
		$nRecCount = $nRecCount + 1;
		if ($nRecCount >= $nStartRec) {
			$nRecActual = $nRecActual + 1;
			$x_MonthYear = $row["MonthYear"];
			$x_ID = $row["ID"];
			$x_PreviousID = $row["PreviousID"];
			$x_Link = $row["Link"];
			$x_XReading = $row["XReading"];
			$x_YesterdayXReading = $row["YesterdayXReading"];
			$x_DiffXreading = $row["DiffXreading"];
			$x_CheckAmount = $row["CheckAmount"];
			$x_LunchAmount = $row["LunchAmount"];
			$x_OverAmount = $row["OverAmount"];
			$x_CreditAmount = $row["CreditAmount"];
			$x_PayoutAmount = $row["PayoutAmount"];
			$x_OtherAdj = $row["OtherAdj"];
			$x_TotalDrawAmount = $row["TotalDrawAmount"];
			$x_PreviousDrawAmount = $row["PreviousDrawAmount"];
			$x_CashDiff = $row["CashDiff"];
			$x_CashAdj = $row["CashAdj"];
			$x_RegisterTotal = $row["RegisterTotal"];
			$x_Short_Over = $row["Short_Over"];
			if ($sExport == "csv") {

				// Field MonthYear
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_MonthYear). "\"" : $x_MonthYear)  . ",";

				// Field ID
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_ID). "\"" : $x_ID)  . ",";

				// Field PreviousID
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_PreviousID). "\"" : $x_PreviousID)  . ",";

				// Field Link
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_Link). "\"" : $x_Link)  . ",";

				// Field XReading
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_XReading). "\"" : $x_XReading)  . ",";

				// Field YesterdayXReading
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_YesterdayXReading). "\"" : $x_YesterdayXReading)  . ",";

				// Field DiffXreading
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_DiffXreading). "\"" : $x_DiffXreading)  . ",";

				// Field CheckAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_CheckAmount). "\"" : $x_CheckAmount)  . ",";

				// Field LunchAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_LunchAmount). "\"" : $x_LunchAmount)  . ",";

				// Field OverAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_OverAmount). "\"" : $x_OverAmount)  . ",";

				// Field CreditAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_CreditAmount). "\"" : $x_CreditAmount)  . ",";

				// Field PayoutAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_PayoutAmount). "\"" : $x_PayoutAmount)  . ",";

				// Field OtherAdj
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_OtherAdj). "\"" : $x_OtherAdj)  . ",";

				// Field TotalDrawAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_TotalDrawAmount). "\"" : $x_TotalDrawAmount)  . ",";

				// Field PreviousDrawAmount
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_PreviousDrawAmount). "\"" : $x_PreviousDrawAmount)  . ",";

				// Field CashDiff
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_CashDiff). "\"" : $x_CashDiff)  . ",";

				// Field CashAdj
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_CashAdj). "\"" : $x_CashAdj)  . ",";

				// Field RegisterTotal
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_RegisterTotal). "\"" : $x_RegisterTotal)  . ",";

				// Field Short_Over
				$sCsvStr .= ($bExcelCSV ? "\"" . str_replace("\"","\"\"", $x_Short_Over). "\"" : $x_Short_Over)  . ",";
				$sCsvStr = substr($sCsvStr,0, strlen($sCsvStr)-1); // Remove last comma
				$sCsvStr .= "\n";
			}
		}
	}

	// Close recordset and connection
	phpmkr_free_result($rs);
	ob_end_clean();
	if ($sExport == "csv") {
		echo  $sCsvStr;
	}
}
?>